package com.pkx.cloud.fse7.dms.token.controller;

import com.pkx.cloud.fse7.dms.token.entity.DoorIssueRequest;
import com.pkx.cloud.fse7.dms.token.entity.LossReport;
import com.pkx.cloud.fse7.dms.token.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Description
 * @Author chenwg
 * @date 2020.11.15 16:11
 */
@RestController
@RequestMapping("/TokenService")
@Api(value = "卡务管理", tags = {"卡务管理"})
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @PostMapping("/LossReport")
    @ApiOperation("挂失")
    private String lossReport(@RequestBody LossReport lossReport) {
        // 初始设置
        init(lossReport);
        lossReport.setTokenOper(5);
        return tokenService.lossReport(lossReport);
    }

    @PostMapping("/UnLost")
    @ApiOperation("解挂")
    private String unLost(@RequestBody LossReport lossReport) {
        // 初始设置
        init(lossReport);
        lossReport.setTokenOper(6);
        return tokenService.unLost(lossReport);
    }

    @PostMapping("/DoorUnregister")
    @ApiOperation("注销")
    private String doorUnregister(@RequestBody LossReport lossReport) {
        // 设置
        init(lossReport);
        lossReport.setTokenOper(12);
        return tokenService.doorUnregister(lossReport);
    }

    @PostMapping("/DoorIssue")
    @ApiOperation("发卡")
    private String doorIssue(@RequestBody DoorIssueRequest doorIssueRequest) {
        // 初始化常量信息
        // 开卡
        doorIssueRequest.setTokenOper(1);
        // 是否有卡
        doorIssueRequest.setIsExistCard(true);
        // 凭证类型 卡片
        doorIssueRequest.setTokenType(1);
        // 车牌 与token设置一致
        doorIssueRequest.setPlate(doorIssueRequest.getTokenID());
        // 设置开卡时间
        doorIssueRequest.setRedate(LocalDateTime.now());
        //
        return tokenService.doorIssue(doorIssueRequest);
    }

    /**
     * 通用参数初始化
     *
     * @param lossReport
     */
    private void init(LossReport lossReport) {
        Integer[] proj = {2};
        lossReport.setSubSystems(proj);
        // 是否有默认卡片
        lossReport.setIsExistCard(true);
        // 门禁卡
        lossReport.setTokenType(1);
        // 下载模式
        lossReport.setUseModel(1);
        // 操作人员编号
        lossReport.setOperNo("1");
        lossReport.setOperName("系统管理员");
        // 操作时间
        lossReport.setRedate(LocalDateTime.now());
        // 是否合法
        lossReport.setIsLegal(true);

    }

}
